package com.gzcc.controller;

import com.gzcc.model.User;
import com.gzcc.repository.UserRepository;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
* 描述: 移动端controller
* @Author: Mr.Zhong
* @Date: 2018/6/13
*/
@Controller
@RequestMapping("/front")
public class IndexController {

    @Autowired
    private UserRepository userRepository;

    /**
    * 描述: 跳转前台userhome页面
    * @Author: Mr.Zhong
    * @Date: 2018/6/13
    */
    @RequestMapping("/userhome")
    public String userhome() {
        return "front-end/userhome.btl";
    }

    /**
    * 描述: 跳转前台userEdit页面
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/userEdit")
    public String userEdit() {
        return "front-end/userEdit.btl";
    }

    /**
    * 描述: 跳转前台index页面
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/index")
    public String index() {
        return "front-end/index.btl";
    }

    /**
    * 描述: 跳转前台views页面
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/views")
    public String views(HttpSession session) {
        if (session.getAttribute("isLogin") == null) {
            System.out.println("你还没有登录！");
            return "front-end/index.btl";
        }
//        if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() == null){
//        }
//        Object object =SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return "front-end/views.btl";
    }


    /**
    * 描述: 加入购物车
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/addCart")
    @ResponseBody
    public Integer addCart(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("loginUserId");
        User user = userRepository.getOne(userId);
        //需要完善信息
        if (user.getAddress() == null || user.getPhone() == null || user.getEmail() == null) {
            return 1;
        }
        //信息完善通過
        //把東西加入購物車
        return 2;
    }


    /**
    * 描述: 编辑用户地址信息
    * @param  address 地址
    * @return User
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/editUsers")
    @ResponseBody
    public User editUser(HttpSession session, String address) {
        Integer userId = (Integer) session.getAttribute("loginUserId");
        Optional<User> userOptional = userRepository.findById(userId);
        User user = userOptional.get();

        user.setAddress(address);
        user.setPhone("phone");
        user.setEmail("dwjadhuwhuda");
        user = userRepository.save(user);
        return user;
    }

    /**
    * 描述: 跳转前台注册页面
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/register")
    public String register() {
        return "front-end/register.btl";
    }

    /**
    * 描述: 跳转前台登录页面
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/login")
    public String login() {
        return "front-end/login.btl";
    }

    /**
    * 描述: 注册方法
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/addregister")
    public String register(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");
        String email = request.getParameter("email");
        if (password.equals(password2)) {
            User user = new User();
            user.setName(username);
            user.setPassword(password);
            user.setEmail(email);
            userRepository.save(user);
            return "front-end/login.btl";
        } else {
            return "front-end/register.btl";
        }
    }

    /**
    * 描述: 登录方法
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping("/addlogin")
    public String login(HttpServletRequest request, HttpSession session) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userRepository.findByNameAndPassword(username, password);
        if (user != null) {
            session.setAttribute("isLogin", "dwadw");
            session.setAttribute("loginUserId", user.getId());
            return "front-end/index.btl";
        } else {
            return "front-end/login.btl";
        }
    }

    /**
    * 描述: 编辑用户
    * @param  id
    * @return
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping(value = "editUser", method = RequestMethod.GET)
    public ModelAndView edit(int id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("front-end/editUser.btl");
        User user = userRepository.getOne(id);
        mv.addObject("user", user);
        return mv;
    }


    /**
    * 描述: 更新用户信息
    * @param
    * @return
    * @Author: Mr.Zhong
    * @Date: 2018/6/14
    */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String updateUser(User user) {
        ModelAndView modelAndView = new ModelAndView();
        userRepository.save(user);
        return "redirect:/user";
    }

}

